
/******************* lex file - final.l ******************/

%{
#include <stdio.h>
#include <stdlib.h>

%}
int yylineno=0;
alpha [A-Za-z]
digit [0-9]
%%
[ \t] 
[\n]        {yylineno++;}
for       return FOR;
switch    return SWITCH;
case return CASE;
break return BREAK;
default return DEFAULT;
do    return DO;
while    return WHILE;
if    return IF;
then    return THEN;
else    return ELSE;
{digit}+    return NUM;
{alpha}({alpha}|{digit})*    return ID;
"<="    return LE;
">="    return GE;
"=="    return EQ;
"!="    return NE;
"||"    return OR;
"&&"    return AND;
.    return yytext[0];

%%

/********************* yacc file - final.y ************************/

%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
extern FILE *yyin;

%}
%token ID NUM WHILE LE GE EQ NE OR AND IF THEN ELSE DO SWITCH CASE DEFAULT BREAK FOR
%right '='
%left AND OR
%left '<' '>' LE GE EQ NE
%left '+''-'
%left '*''/'
%right UMINUS
%left '!'
%%
S       : ST1 {printf("Input while accepted.\n");exit(0);}
        | ST2 {printf("Input if-else accepted.\n");exit(0);}
        | ST4 {printf("Input do-while accepted.\n");exit(0);}
        | ST6 {printf("Input switch-case accepted.\n");exit(0);}
        | ST7 {printf("Input For-loop accepted.\n");exit(0);}
        | ST8 {printf("Input if accepted.\n");exit(0);}
        ;
      
ST1     : WHILE'(' E2 ')' '{' ST '}'
ST      : ST ST
		|ST2
        | E';' 
        ;
ST2     : IF '(' E2 ')' THEN ST3';' ELSE ST3';'
        ;
ST8     : IF '(' E2 ')' THEN ST9';'
        ;
ST3  : ST2
     | E
     ;
ST9  : ST8
     | E
     ;
ST4  : DO '{' ST '}' WHILE'(' E2 ')'';'; 
ST5  :     ST5 ST5
     | E';'
     ;  
ST6     :    SWITCH '(' ID ')' '{' B '}'
         ;
   
B       :    C
         |    C    D
        ;
   
C      :    C    C
        |    CASE NUM ':' E ';'
        | BREAK ';'
        ;

D      :    DEFAULT    ':' E ';' BREAK ';'
        ; 
ST7       : FOR '(' E ';' E2 ';' E ')' DEF
           ;
DEF    : '{' BODY '}'
           | E';'
           | ST7
           |
           ;
BODY  : BODY BODY
           | E ';'       
           | ST7
           |            
           ;
E        : ID '=' E
          | E '+' E
          | E '-' E
          | E '*' E
          | E '/' E
          | E '<' E
          | E '>' E
          | E LE E
          | E GE E
          | E EQ E
          | E NE E
          | E OR E
          | E AND E
          | E '+' '+'
          | E '-' '-'
          | ID 
          | NUM
          ;
E2        : E'<'E
          | E'>'E
          | E LE E
          | E GE E
          | E EQ E
          | E NE E
          | E OR E
          | E AND E
          | ID
          | NUM
          ;

%%
#include "lex.yy.c"
int yyerror(char *s){
    extern int yylineno;
    fprintf(stderr,"At line %d %s ",--yylineno,s);  
    return 0;
}

int yywrap(){
    return 1;
}

int main(int argc, char *argv[]){
    yyin=fopen("input4yacc.c","r");
    yyparse();
    fclose(yyin);
    return 0;
}


/*************************Input file*****************************/

while(c<d)
{
if(c<10)then
c=a+b;
else 
x=y+z
}

/********************output**************************/

student@admin7-OptiPlex-360:~$ cd Desktop
student@admin7-OptiPlex-360:~/Desktop$ lex final.l
student@admin7-OptiPlex-360:~/Desktop$ yacc final.y
yacc: 3 rules never reduced
yacc: 21 shift/reduce conflicts, 2 reduce/reduce conflicts.
student@admin7-OptiPlex-360:~/Desktop$ gcc y.tab.c -ll
student@admin7-OptiPlex-360:~/Desktop$ ./a.out
At line 6 syntax error 
student@admin7-OptiPlex-360:~/Desktop$ 


